package com.alibaba.springboot.health.service.impl;

import com.alibaba.springboot.health.dao.RoleDao;
import com.alibaba.springboot.health.pojo.Role;
import com.alibaba.springboot.health.service.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * (Role)表服务实现类
 *
 * @author makejava
 * @since 2019-12-24 14:33:36
 */
@Service("roleService")
public class RoleServiceImpl implements RoleService {
    private static final Logger LOGGER= LogManager.getLogger(RoleServiceImpl.class);
    @Autowired
    private RoleDao roleDao;

    /**
     * 通过ID查询单条数据
     *
     * @param roleId 主键
     * @return 实例对象
     */
    @Override
    public Role queryById(Integer roleId) {
        return roleDao.queryById(roleId);
    }

    @Override
    public Role queryByUserName(String userName) {
        LOGGER.info("11111111111111111111111"+roleDao.queryByUserName(userName));
        return roleDao.queryByUserName(userName);
    }


    /**
     * 查询多条数据
     * @param page 页码
     * @param pageSize 页大小
     * @param role 实体
     * @return PageInfo<Role> 分页数据集合
     */
    @Override
    public PageInfo<Role> queryAll(int page, int pageSize, Role role) {
        /*拦截分页信息*/
        PageHelper.startPage(page, pageSize);

        List<Role> roleList = roleDao.queryAll(role);

        return new PageInfo<>(roleList);
    }

    /**
     * 新增数据
     *
     * @param role 实例对象
     * @return 实例对象
     */
    @Override
    public Role insert(Role role) {
        roleDao.insert(role);
        return role;
    }

    /**
     * 修改数据
     *
     * @param role 实例对象
     * @return 实例对象
     */
    @Override
    public Role update(Role role) {
        roleDao.update(role);
        return queryById(role.getRoleId());
    }

    /**
     * 通过主键删除数据
     *
     * @param roleId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer roleId) {
        return roleDao.deleteById(roleId) > 0;
    }
}